<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <button id="btn1">11111111</button>
    <script>
      const bucket = new WeakMap();
      const data = {ok:true, text: "hello world" };
      let activeEffect;
      function effect(fn) {
        const effectFn=()=>{
          activeEffect = effectFn;
          fn();
        }
        effectFn.deps =[];
        effectFn();
      }
      const obj = new Proxy(data, {
        get(target, key) {
          console.log("get",key)
          track(target, key);
          return target[key];
        },
        set(target, key, newVal) {
          console.log("set",key)
          target[key] = newVal;
          trigger(target,key);
        },
      });
      function track(target,key){
        if(!activeEffect) return;
        let depsMap = bucket.get(target);
        if(!depsMap){
            bucket.set(target,(depsMap = new Map()))
        }
        let deps=depsMap.get(key);
        if(!deps){
            depsMap.set(key,(deps = new Set()))
        }
        deps.add(activeEffect);
        activeEffect.deps.push(deps);
      }

      function trigger(target,key){
        const depsMap = bucket.get(target);
        if(!depsMap) return;
        const effects = depsMap.get(key);
        effects && effects.forEach(fn=>fn());
      }

      effect(function effectFn() {
        console.log("effect")
        document.body.innerText = obj.ok ? obj.text :'not';
      });

      setTimeout(() => {
        obj.ok =false;
      }, 1000);

      setTimeout(() => {
        // console.log("obj.text")
        obj.text = 8888;
      }, 3000);
    </script>
  </body>
</html>
